package com.xuxin.nantai.model;

import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.DefaultGroupBean;
import com.xuxin.nantai.bean.FlowingCodeBean;
import com.xuxin.nantai.bean.GroupBean;
import com.xuxin.nantai.bean.InspectorBean;
import com.xuxin.nantai.contract.CompleteUpdateContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.CompleteUpdatePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * Created by xu_xin
 * Created on 2020.06.08 0008
 */
public class CompleteUpdateModel implements CompleteUpdateContract.Model {
    @Override
    public void getInspector(CompleteUpdatePresenter presenter, String loginName, String TeamType) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getInspector(loginName, TeamType)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<InspectorBean>() {
            @Override
            public void onNext(InspectorBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getFlowingCode(CompleteUpdatePresenter presenter, String FlowingCode) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getFlowingCode(FlowingCode)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<FlowingCodeBean>() {
            @Override
            public void onNext(FlowingCodeBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getGroups(CompleteUpdatePresenter presenter) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getGroups()
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<GroupBean>() {
            @Override
            public void onNext(GroupBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getDefaultGroup(CompleteUpdatePresenter presenter, String LoginName) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getDefaultGroup(LoginName)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<DefaultGroupBean>() {
            @Override
            public void onNext(DefaultGroupBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void update(CompleteUpdatePresenter presenter,
                       String LoginName,
                       String UserName,
                       String Guid,
                       String ID,
                       String cInvCode,
                       String cInvName,
                       String PackQty,
                       String ComNum,
                       String CheckNum,
                       String InspectorCode,
                       String InspectorName,
                       String InspectTime,
                       String FacArea,
                       String TeamType,
                       int IsReCheck,
                       String FlowingCode,
                       String GroupName,
                       String GroupCode) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .update(LoginName,
                        UserName,
                        Guid,
                        ID,
                        cInvCode,
                        cInvName,
                        PackQty,
                        ComNum,
                        CheckNum,
                        InspectorCode,
                        InspectorName,
                        InspectTime,
                        FacArea,
                        TeamType,
                        IsReCheck,
                        FlowingCode,
                        GroupName,
                        GroupCode)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(BaseBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }
}